import {
  SET_BATCH_ID,
  SET_START_TIME,
  SET_END_TIME,
  GET_DATA, 
  GET_DATA_SUCCESS,
  GET_DATA_FAIL,
  SET_PAGE_NUM,
  SET_CONTENT,
  SET_STATUS
} from '@/actions/batchRecord'
import moment from 'moment'
import util from '@/utils';

const initState = {
  startTime: util.getStartTime(moment()),
  endTime: util.getEndTime(moment()),
  content: "", // 短信内容
  status: 2, // 审核状态 默认审核通过
  pageNum: 1,
  pageSize: 10,
  batchId: '',
  data: [],
  total: 0,
  loading: false,
}

export default function batchRecordReducer(state = initState, action) {
  switch (action.type) {
    case SET_BATCH_ID:
      return { ...state, batchId: action.batchId}
      break;

    case SET_CONTENT:
      return { ...state, content: action.content}
      break;

    case SET_STATUS:
      return { ...state, status: action.status}
      break;

    case SET_START_TIME:
      return { ...state, startTime: util.getStartTime(action.startTime) }
      break;

    case SET_END_TIME:
      return { ...state, endTime: util.getEndTime(util.getEndTime(action.endTime)) }
      break;

    case GET_DATA:
      return { ...state, loading: true }
      break;

    case GET_DATA_SUCCESS:
      const { list, total } = action.data
      return { ...state, data: list, total, loading: false }
      break

    case GET_DATA_FAIL:
      return { ...state, loading: false }
      break

    case SET_PAGE_NUM:
      return { ...state, pageNum: action.pageNum }
      break;
  
    default:
      return state
      break;
  }
}